package com.javabasic2.pjchatserver;
import java.net.ServerSocket;
import java.net.Socket;

public class ListeningThread extends Thread {
	
	private ChatFrame frame;
	private ServerSocket listener;
	
	private boolean running = false;
	
	//public ListeningThread() {}
	public ListeningThread(ChatFrame frame) {
		this.frame = frame;
		try {
			listener = new ServerSocket(9999, 100);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	@Override
	public void run() {
		running = true;
		while (running) {
			try {
				Socket socket = listener.accept();
				// 연결된 소켓을 frame에 전달
				NetUnit unit = new NetUnit(frame, socket);
				unit.start();
				//frame.getClients().add(unit);
				frame.addClient(unit);
				
			} catch (Exception ex) {
				//System.out.println("error-message")
				ex.printStackTrace();
			}
		}
	}	
	public void doStop() {
		running = false;//thread 반복문을 중단시키는 코드
		try {
			listener.close();
		} catch (Exception ex){}
	}

}






